package com.cityos.android.mystudynestedscroll.common.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.WindowManager;

/**
 * @author hailong
 * @date 2020年07月07日 7:58 PM
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
public class DisplayUtils {


    public static int dp2px(Context context, float dipValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, context.getResources().getDisplayMetrics());
    }

    public static int dp2px(Resources resources, float dipValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, resources.getDisplayMetrics());
    }

    public static int px2dp(Context context, float pxValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5F);
    }

    public static int px2dp(Resources resources, float pxValue) {
        float scale = resources.getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5F);
    }

    public static int sp2px(Context context, float spValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, context.getResources().getDisplayMetrics());
    }

    public static float sp2px_float(Context context, float spValue) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, context.getResources().getDisplayMetrics());
    }

    public static Point getScreenMetrics(Context context) {
        DisplayMetrics dm       = context.getResources().getDisplayMetrics();
        int            w_screen = dm.widthPixels;
        int            h_screen = dm.heightPixels;
        return new Point(w_screen, h_screen);
    }

    public static int getScreenWidth(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRealMetrics(dm);
        return dm.widthPixels;
    }

    public static int getScreenHeight(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRealMetrics(dm);
        return dm.heightPixels;
    }

    public static float getScreenRate(Context context) {
        Point P = getScreenMetrics(context);
        float H = (float) P.y;
        float W = (float) P.x;
        return H / W;
    }

    public static int getRealWidth(Context context, float dipValue) {
        return getScreenWidth(context) - dp2px(context, dipValue) * 2;
    }

    public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }


}
